package com.xn.app.muying;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xn.app.muying.fragment.AdviceFragment;
import com.xn.app.muying.fragment.MonitorFragment;
import com.xn.app.muying.fragment.MineFragment;
import com.xn.app.muying.fragment.OrderFragment;
import com.xn.app.muying.fragment.HomeFragment;

public class MainActivity extends AppCompatActivity {
    RadioGroup tab_rg;
   public static  Activity mActivity;
    private long mExitTime;
    private Fragment mContent;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private FragmentManager ragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity=MainActivity.this;
        tab_rg = (RadioGroup)findViewById(R.id.main_rg);
        tab_rg.setOnCheckedChangeListener(checkedChangeListener);
//        HomeFragment homeFragment = new HomeFragment();
//        topass(homeFragment);
//        initContent();
        initFragment();
        IntentFilter filter=new IntentFilter("deng_lu_cheng_gong");
        registerReceiver(broadcastReceiver,filter);
    }
    public void initFragment(){
        mineFragment = new MineFragment();
        homeFragment = new HomeFragment();
        ragmentManager = getSupportFragmentManager();



//        FragmentTransaction ft1 = ragmentManager.beginTransaction();
//        ft1.add(R.id.main_frame,mineFragment).commit();

        FragmentTransaction ft = ragmentManager.beginTransaction();
        ft.add(R.id.main_frame,homeFragment).commit();
        mContent = homeFragment;
    }
    public void switchContent(Fragment to) {
        if (mContent != to) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) {	// 先判断是否被add过
                transaction.hide(mContent).add(R.id.main_frame, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                mContent = to;
            } else {
                transaction.hide(mContent).show(to).commit(); // 隐藏当前的fragment，显示下一个
                mContent = to;
            }
        }
    }

    RadioGroup.OnCheckedChangeListener checkedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int i = 0; i < 2; i++) {
                RadioButton tabrb = (RadioButton) group.getChildAt(i);
                if (tabrb.isChecked()) {
                    tabrb.setTextColor(getResources().getColor(R.color.sys_fen));
                } else {
                    tabrb.setTextColor(getResources().getColor(R.color.black_white));
                }
            }
            switch (checkedId) {
                case R.id.navigation_bar_item_home:
                    switchContent(homeFragment);
//                    HomeFragment homefragment =  new HomeFragment();
//                   topass(homefragment);
                    break;

//                case R.id.navigation_bar_item_monitor:
//                    MonitorFragment reMenFragment = new MonitorFragment();
//                    topass(reMenFragment);
//                     break;
//
//                case R.id.navigation_bar_item_advice:
//                    AdviceFragment adviceFragment = new AdviceFragment();
//                    topass(adviceFragment);
//                    break;
//
//                case R.id.navigation_bar_item_order:
//                    OrderFragment orderFragment = new OrderFragment();
//                    topass(orderFragment);
//                    break;


                case R.id.navigation_bar_item_mine:
                    switchContent(mineFragment);
//                    MineFragment jpfragment = new MineFragment();
//                    topass(jpfragment);
                    break;
            }
        }
    };



    public void topass(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.main_frame, fragment);
        ft.commitAllowingStateLoss();
    }
    BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           String action= intent.getAction();
            Log.i("broadcastReceiver","==========action=========="+action);
            if (!TextUtils.isEmpty(action)){
                if (TextUtils.equals("deng_lu_cheng_gong",action)){
                    Log.i("dakai","========sssssssssss=======");
                    MineFragment jpfragment = new MineFragment();
                    topass(jpfragment);
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();

            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
